package com.jiayou.Tank;

import com.jiayou.Bullet.Bullet1;
import com.jiayou.Map.Body;
import com.jiayou.Map.Gang;
import com.jiayou.Map.Home;
import com.jiayou.Map.Zhuan;
import com.jiayou.MyFrame;
import com.jiayou.tools.Constclass;
import com.jiayou.tools.TheGameLoading;

import java.awt.*;
import java.lang.invoke.SwitchPoint;
import java.util.List;

public class Tank2 extends Model {
    private int random;
    public Image imageup = TheGameLoading.getImage("tank1/1.png");
    public Image imagedown= TheGameLoading.getImage("tank1/4.png");
    public Image imageleft= TheGameLoading.getImage("tank1/2.png");
    public Image imageright= TheGameLoading.getImage("tank1/3.png");
    public Image image = imagedown;
    public Tank2(int x, int y, int width, int heigh, int speed ) {
        super(x, y, width, heigh, speed);
        this.random = 350;
    }
    public Tank2(int x, int y, int width, int heigh, int speed,int random) {
        super(x, y, width, heigh, speed);
        this.random = random;
    }

    private void move() {
         if(sw==Constclass.LEFT)
        {
            if (x>=10) {
                image = imageleft;
                x -= speed;
                if ((int)(1+Math.random()*(random-1+1))==10)
                {
                    sw= Constclass.UP;
                }
            }
            else
                sw= Constclass.UP;
        }
        else if(sw==Constclass.RIGHT)
        {
            if ( x<=Constclass.WEIGHT-40) {
                image = imageright;
                x += speed;
                if ((int)(1+Math.random()*(random-1+1))==10)
                {
                    sw = Constclass.DOWN;
                }
            }
            else
                sw = Constclass.DOWN;
        }
        else if (sw==Constclass.UP)
        {
            if (  y>=35) {
                image = imageup;
                y -= speed;
                if ((int)(1+Math.random()*(random-1+1))==10)
                {
                    sw = Constclass.RIGHT;
                }
            }
            else
                sw = Constclass.RIGHT;
        }
        else if (sw == Constclass.DOWN)
        {
            if ( y<=Constclass.HEIGHT-40) {
                image = imagedown;
                y += speed;
                if ((int)(1+Math.random()*(random-1+1))==10)
                {
                    sw = Constclass.LEFT;
                }
            }
            else
                sw = Constclass.LEFT;
        }

    }
    public void draw(Graphics graphics){
        Color color = graphics.getColor();
        graphics.setColor(Color.white);
        graphics.setColor(color);
        graphics.drawImage(image,x,y,width,heigh,null);
        move();
    }

    @Override
    public void draw(Graphics graphics, List<Body> list,Tank1 tank1) {
        draw(graphics);
        shot(graphics);
        if(bullet1!=null){
        for (Body body : list) {
            if (bullet1.rec().intersects(body.rec())) {
                if (body instanceof Gang) {
//                    list.remove(body);
                    bullet1 = null;
                } else if (body instanceof Zhuan) {
                    list.remove(body);
                    bullet1 = null;
                } else if (body instanceof Home) {
                    list.remove(body);
                    bullet1 = null;
                    ((Home) body).image = TheGameLoading.getImage("over.png");
                    MyFrame.S = false;
                }
                break;
            }

            if(tank1.rec().intersects(bullet1.rec()))
            {
                System.out.println("GAME OVER!");
                MyFrame.S = false;
            }
        }
        }
    }

    public void shot(Graphics graphics)
    {
        if (bullet1==null)
        {
            if ((int)(1+Math.random()*(350-1+1))==350) {
                bullet1 = new Bullet1(x, y, 5, sw);
                bullet1.draw(graphics);
            }
            else
                return;
            if(bullet1.x<=0 || bullet1.x>=Constclass.WEIGHT || bullet1.y>=Constclass.HEIGHT || bullet1.y<=0)
            {
                bullet1 = null;
            }
        }
        else
        {
            bullet1.draw(graphics);
            if(bullet1.x<=0 || bullet1.x>=Constclass.WEIGHT || bullet1.y>=Constclass.HEIGHT || bullet1.y<=0)
            {
                bullet1 = null;
            }
        }
    }

}
